/**
 * @file response.h
 * @brief Header file for response manager
 *
 * @author Ricky Hariady (ricky.hariady@enter.web.id)
 *
 * @date 7/10/2013
 */

#define RESPONSE_H

#ifndef PINTAROS_H
# include "pintaros.h"
#endif

#define Response_OK                                     0x9000

#define Response_Normal                                 0x6100

#define Response_Warning_Unchanged                      0x6200
#define Response_Warning_DataCorrupt                    0x6281
#define Response_Warning_EndOfFile                      0x6282
#define Response_Warning_FileDeactivated                0x6283

#define Response_Warning_Changed                        0x6300
#define Response_Warning_FilledUp                       0x6381

#define Response_Error_Unchanged                        0x6400
#define Response_Error_Changed                          0x6500

#define Response_WrongLength                            0x6700

#define Response_NotSupported                           0x6800
#define Response_NotSupported_LogicalChannel            0x6881
#define Response_NotSupported_SecureMessaging           0x6882
#define Response_NotSupported_LastCommandExpected       0x6883
#define Response_NotSupported_CommandChain              0x6884

#define Response_CmdNotAllowed                          0x6900
#define Response_CmdNotAllowed_Incompatible_FS          0x6981
#define Response_CmdNotAllowed_SecurityStatus           0x6982
#define Response_CmdNotAllowed_AuthBlocked              0x6983
#define Response_CmdNotAllowed_RefDataNotUsable         0x6984
#define Response_CmdNotAllowed_ConditionNotSatisfied    0x6985
#define Response_CmdNotAllowed_NoCurrentEF              0x6986
#define Response_CmdNotAllowed_ExpectSecureMsg          0x6981
#define Response_CmdNotAllowed_IncorrectSecureMsg       0x6981

#define Response_WrongP1P2                              0x6A00
#define Response_WrongP1P2_IncorrectData                0x6A80
#define Response_WrongP1P2_FuncNotSupported             0x6A81
#define Response_WrongP1P2_FileNotFound                 0x6A82
#define Response_WrongP1P2_RecordNotFound               0x6A83
#define Response_WrongP1P2_NotEnoughMem                 0x6A84
#define Response_WrongP1P2_NCInconsistentTLV            0x6A85
#define Response_WrongP1P2_IncorrectP1P2                0x6A86
#define Response_WrongP1P2_NCInconsistentP1P2           0x6A87
#define Response_WrongP1P2_RefDataNotFound              0x6A88
#define Response_WrongP1P2_FileExist                    0x6A89
#define Response_WrongP1P2_DFNameExist                  0x6A8A

#define Response_INSNotSupported                        0x6D00
#define Response_CLANotSupported                        0x6E00
#define Response_FatalError                             0x6F00

#define MAX_RESPONSE    20      ///< Maximum Response Type

/*****************************/
/*        Enumeration        */
/*****************************/

/**
 * @brief response type enumeration
 */
typedef enum
{
        OK,                             ///< 9000
        WrongLength,                    ///< 6700
        CmdNotAllowed,                  ///< 6986
        FileNotFound,                   ///< 6A82
        WrongP1P2,                      ///< 6Axx
        INSNotSupported,                ///< 6D00
        CLANotSupported,                ///< 6E00
        FatalError,                     ///< 6F00
        MaxResponse = MAX_RESPONSE      ///< End of Response Enumeration
} rspn_type;

/********************************/
/*        Data Structure        */
/********************************/

/**
 * @brief structure of response map
 */
struct  rspn_maps_struct
{
        uint8_t SW1;       ///< SW 1 for response APDU
        uint8_t SW2;       ///< SW 2 for response APDU
        bool    xtra_flag; ///< indication if the response need extra parameter for SW2
};

/**************************/
/*        Variable        */
/**************************/

struct  rspn_maps_struct        rspn_maps[MAX_RESPONSE]; ///< response maps

/**************************/
/*        Function        */
/**************************/

/**
 * @brief set up an appropriate response APDU
 *
 * Call by CommandInterpreter() when finish execute the command.
 * Interpret response type from command handler to Return Code (SW1 SW2),
 * then transmit response APDU (Return Code plus Return data) over transTx()
 *
 * @param *apdu_res     pointer to apdu resources
 * @return none
 */
void ResponseManager(struct buffer_struct *tx_buffer, struct apdu_res_struct *apdu_res); 
